package com.nextgenapp.fbgraph.post
{
	import com.nextgenapp.fbgraph.GraphApi;
	import com.nextgenapp.fbgraph.common.GraphConst;
	
	import flash.external.ExternalInterface;
	
	import mx.controls.Alert;
	
	/**
	 * post feed.  
	 */
	public class PostFeed
	{
		public static var apiUrl:String = GraphConst.PROTOCOL + "://graph.facebook.com/{profile_id}/feed";
		
		public var access_token:String = null;
		
		public function PostFeed()
		{
		}
		
		
//		public function postStatusFeed(recipientUid:String, message:String):void {
//			
//			url = apiUrl.replace("{profile_id}", recipientUid);
//			var message:String = message;
//			var picture:String = null;
//			var link:String = null;
//			var name:String = null;
//			var description:String = null;
//			postFeed(message, picture, link, name, description);
//		}
		
		
		public function postFeed(recipientUid:String, message:String, picture:String, link:String, name:String, description:String):void {
			
			if (postFeedEnabled()) {
				var url:String = apiUrl.replace("{profile_id}", recipientUid);
				var parameters:Object = {};
						
	            if (access_token) {
	            	// message, picture, link, name, description
	            	if (message != null) { parameters.message = message; }
	            	if (picture != null) { parameters.picture = picture; }
	            	parameters.swf = picture;
	            	parameters.flash = picture;
	            	if (link != null) { parameters.link = link; }
	            	if (name != null) { parameters.name = name; }
	            	if (description != null) { parameters.description = description; }
		            //parameters.access_token = access_token;
		            trace("postFeed to " + url + ".  message="+message+".  picture="+picture+".  link="+link+".  name="+name+".  description="+description);
					var graphApi:GraphApi = new GraphApi();
					graphApi.write(url, parameters);

		        } else {
		        	Alert.show("Error: access_token not provided.  Please report this to support@nextgenapp.com");
		        }
		 	}
		}

		/**
		 * return whether post feed is enabled.  
		 * default is enabled.  disable by specify ?post_feed=false  in url.
		 */
		private function postFeedEnabled():Boolean {
			var enabled:Boolean = true;
			var href:String = ExternalInterface.call("window.location.href.toString"); // this works.  but require allowScriptAccess="always"
			if (href.indexOf("post_feed=false") != -1) {
				enabled = false;
			}
			return enabled;
		}
	}
}